"""
解析数据到智图和A表
"""
import os
import shutil

import facade
from parsel import Selector
from xjlibrary.mdatetime.mtime import getTodayDate
from xjlibrary.mdatetime.mtime2 import MDateTimeUtils
from xjlibrary.our_file_dir import BaseDir
from xjlibrary.tools.BaseString import BaseString

from id_encode import GetLngid

curpath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curpath, -2)
dirPath = BaseDir.get_new_path(TopPath, "download", "yuetongbook", "download", "bookhtml")
db3dir = BaseDir.get_new_path(TopPath, "download", "yuetongbook", "download", "db3")
BaseDir.create_dir(db3dir)
configfile = BaseDir.get_new_path(curpath, "db.ini")
sPath = BaseDir.get_new_path(curpath, "template")


class ParaZTA(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)
        self.Listpara = []
        self.templatedb3 = BaseDir.get_new_path(sPath, "zt_template.db3")
        self.new_db3_path = self.copyTempDB3(self.templatedb3, db3dir)
        self.db3_zt = facade.Sqlite3Utiles(logger=self.logger).Sqlite3DBConnectFromFilePath(
            self.new_db3_path, encoding="utf-8")

    def copyTempDB3(self, temp_path, copy_out):
        """
        拷贝创建新的db3文件
        :param temp_path: 模板db3文件路径
        :param copy_out: 新的db3输出路径
        :return: 新db3路径
        """
        today_str = str(getTodayDate())
        if not os.path.isfile(temp_path) or not os.path.isdir(copy_out):
            raise FileNotFoundError
        new_db3_name = 'zt_yuetongbook_' + today_str + '.db3'
        new_db3_path = os.path.join(copy_out, new_db3_name)
        shutil.copyfile(temp_path, new_db3_path)
        return new_db3_path

    def select_files(self):
        for file in BaseDir.get_dir_all_files(dirPath):
            print(file)
            rawid = file.split("\\")[-1].replace(".html", "")
            html = BaseDir.single_read_file(file)
            self.para_html(html, rawid)
        self.insert_db3()

    def insert_db3(self):
        sql = "insert or ignore into modify_title_info_zt (`lngid`,`rawid`,`title`,`creator`,`identifier_pisbn`," \
              "`country`,`language`,`cover`,`provider`,`batch`," \
              "`type`,`medium`,`provider_url`,`provider_id`,`date`," \
              "`date_created`,`price`,`provider_subject`,`description`,publisher,description_unit) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

        self.db3_zt.ExeSqlliteMany(sql, self.Listpara)
        self.Listpara.clear()

    def select_db(self, rawid):
        sql = "select coverstat,url from book where rawid='{}'".format(rawid)
        rows = self.mysqlutils.SelectFromDB(sql)
        cover = ""
        if rows[0][0] == 1:
            cover = "/smartlib/yuetongbook/" + rawid.lower() + ".jpg"
        return cover, rows[0][1]

    def para_html(self, html, rawid):

        cover, url = self.select_db(rawid)

        select = Selector(text=html)
        title = select.xpath('.//div[@class="minna_xxx_st02"]/span[@class="spakk01"]/text()').get()
        title = title.strip()
        span_tag_list = select.css('div[class="minna_xxx_st02"]').xpath(".//span")
        pub_date = ""
        author_org = ""
        isbn = ""
        price = ""
        provider_subject = ""
        for span_tag in span_tag_list:
            print(span_tag.get())
            strings = "".join(span_tag.xpath(".//text()").getall())
            if strings.find("定价") > -1:
                price = strings.replace("定价：", "").strip()
            elif strings.find("ISBN") > -1:
                isbn = strings.replace("ISBN：", "").strip()
                isbn = isbn.replace("NULL", "")
                isbn = isbn.replace("-", "")
            elif strings.find("作者") > -1:
                # 交通部公路司;刘浩学，李晓霞
                author_org = strings.replace("作者：", "").strip()
            elif strings.find("所属分类") > -1:
                # 交通部公路司;刘浩学，李晓霞
                provider_subject = strings.replace("所属分类：", "").strip()
            elif strings.find("出版时间") > -1:
                # 交通部公路司;刘浩学，李晓霞
                pub_date = strings.replace("出版时间：", "").strip()
        # 摘要
        description = select.xpath('.//div[@class="minnavtabxbbbg"]/span/p/text()').get()
        if description:
            description = description.strip()
        else:
            description = ''
        author_org = author_org.replace(",", ";").replace("，", ";").replace("NULL", "").replace("0", "")
        author = BaseString.cleanSemicolon(author_org)
        BaseString.cleanSemicolon(author)

        if pub_date:
            pub_date = pub_date.replace("年", "").replace("月", "")
            pub_date = pub_date + "00"
            pub_year = pub_date[:4]
        else:
            pub_year = "1900"
            pub_date = "19000000"

        sub_db_id = "00131"
        product = "YUETONG"
        sub_db = "TS"
        provider_zt = "yuetongbook"
        provider = "CCPCL"

        url = "http://zt.yuetong.cn" + url
        bacth = MDateTimeUtils.get_today_date_strings() + "00"
        type_ = "1"
        medium = "2"
        provider_url = provider_zt + "@" + url
        provider_id = provider_zt + "@" + rawid
        country = "CN"
        language = "ZH"
        publisher = "人民交通出版社"

        list_unit = select.xpath('.//*[@id="bookchapter"]//text()').getall()
        description_unit = ""
        for unit in list_unit:
            unit = unit.replace("\n", "").replace("\t", "")
            if unit:
                description_unit = description_unit + "\n" + unit

        lngid = GetLngid(sub_db_id, rawid)

        self.Listpara.append((lngid, rawid, title, author, isbn,
                              country, language, cover, provider_zt, bacth,
                              type_, medium, provider_url, provider_id, pub_year,
                              pub_date, price, provider_subject, description, publisher, description_unit))


if __name__ == "__main__":
    """
    本库不解析a层
    """
    para = ParaZTA()
    para.select_files()
